package com.songcha.library_common.util

import java.text.SimpleDateFormat
import java.util.*

class TimeUtil {
    companion object{
        @JvmStatic
        fun getTimeByMillSecond(ms:Int?):String{
            if(ms == null) return ""
            val s:Int = ms / 1000
            var str = ""
            if(s < 60){
                str = String.format("00:%02d",s)
            }else{
                val minute = (s / 60)
                val second = s % 60
                str = String.format("%02d:%02d",minute,second)
            }

            return str

        }

        @JvmStatic
        fun getDayInWeek():Int{
            val calendar = Calendar.getInstance()
            val day = calendar.get(Calendar.DAY_OF_WEEK)
            return day
        }

        @JvmStatic
        fun getCurrentHour():Int{
            val calendar = Calendar.getInstance()
            calendar.time = Date(getCurrentTime())
            val hour = calendar.get(Calendar.HOUR_OF_DAY)
            return hour
        }

        @JvmStatic
        fun formatTimeFullString(timeStamp:Long):String{
            val sdf = SimpleDateFormat("yyyy-MM-dd HH:mm:ss")
            return sdf.format(Date(timeStamp))
        }

        @JvmStatic
        fun formatTimeString(timeStamp:Long,format:String):String{
            val sdf = SimpleDateFormat(format)
            return sdf.format(Date(timeStamp))
        }

        @JvmStatic
        fun formatTimeHHmm(timeStamp:Long):String{
            val sdf = SimpleDateFormat("HH:mm")
            return sdf.format(Date(timeStamp))
        }

        @JvmStatic
        fun formatTimeYYYYMMDD(timeStamp:Long):String{
            val sdf = SimpleDateFormat("yyyy-MM-dd")
            return sdf.format(Date(timeStamp))
        }

        @JvmStatic
        fun formatTime(timeStamp:Long,format:String):String{
            val sdf = SimpleDateFormat(format)
            return sdf.format(Date(timeStamp))
        }

        /***
         * get today zero timestamp
         */
        @JvmStatic
        fun getTodayZeroTimeStamp():Long{
            //Greenwich time start with 1970-1-1:0:0，chiness time start with 1970-1-1:8:0
            val curTime = getCurrentTimeSecond()
            val todayStartTime = ((curTime + 28800) / 86400).toInt() * 86400 - 28800
            return todayStartTime.toLong()
        }

        @JvmStatic
        fun getCurrentTimeSecond():Long{
            return getCurrentTime() / 1000
        }

        @JvmStatic
        fun getCurrentTime():Long{
            return System.currentTimeMillis()
        }

        @JvmStatic
        fun dateString2Stamp(dateStr:String?):Long{
            if(dateStr.isNullOrBlank()) return 0
            val dateFormat = SimpleDateFormat("yyyy-MM-dd HH:mm")
            try {
                val date = dateFormat.parse(dateStr)
                return date.time
            } catch (e:Exception) {
               e.printStackTrace()
            }
            return 0
        }

        @JvmStatic
        fun timeStamp2RecentDay(timestamp:Long):String{
            val diff = getCurrentTime() - timestamp
            if(diff <= 1000) {
                return "现在"
            }else if(diff < 3 * 60 * 1000){
                return "刚刚"
            }else if(diff < 60 * 60 * 1000){
                val t = Math.ceil(diff / (60 * 60 * 1000f).toDouble()).toInt()
                return "${t}分钟前"
            }else if(diff < 24 * 60 * 60 * 1000){
                val t = Math.ceil(diff / (24 * 60 * 60 * 1000f).toDouble()).toInt()
                return "${t}小时前"
            }else if(diff<3*24*60*60*1000){
                val t =  Math.ceil(diff / (3 * 24 * 60 * 60 * 1000f).toDouble()).toInt()
                return "${t}天前"
            }else{
                val str = formatTimeFullString(timestamp)
                if(str.length > 10)
                    return str.substring(0,10)
                else
                    return str
            }
        }

        @JvmStatic
        fun excludeSecond(ts:Long):Long{
            return (Math.floor(ts / 60000.0)).toLong() * 60000
        }

        @JvmStatic
        fun excludeMillisSecond(ts:Long):Long{
            return (Math.floor(ts / 1000.0)).toLong() * 1000
        }

    }
}